package com.konjac.service.impl;

import com.konjac.dao.mapper.AddOrderMapper;
import com.konjac.dao.mapper.ShowBookMapper;
import com.konjac.dao.mapper.ShowSupplierMapper;
import com.konjac.dao.mapper.UserMapper;
import com.konjac.pojo.AddOrder;
import com.konjac.pojo.BookInfo;
import com.konjac.pojo.ServiceState;
import com.konjac.pojo.Supplier;
import com.konjac.service.AddOrderService;
import com.konjac.until.Handle;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class AddOrderServiceImpl implements AddOrderService {
    //进货: 根据某种书籍的库存量及销售情况确定进货数量，根据供应商报价选择供应商。输出一份进货单并自动修改库存量，把本次进货的信息添加到进货表中
    //展示书籍信息
    public List<BookInfo> showBookInfo() throws IOException {
        //加载mybaits核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //调用dao层返回书籍的信息
        ShowBookMapper mapper = sqlSession.getMapper(ShowBookMapper.class);
        List<BookInfo> bookInfos = mapper.selectAll();
        //System.out.println(bookInfos);
        sqlSession.close();
        return bookInfos;
    }

    //展示供应商
    public List<Supplier> showSupplier() throws IOException{
        //加载mybaits核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //调用dao层返回供应商的信息
        ShowSupplierMapper mapper = sqlSession.getMapper(ShowSupplierMapper.class);
        List<Supplier> suppliers = mapper.selectAll();
        sqlSession.close();
        return suppliers;
    }

    //添加订单信息
    public ServiceState AddOrder(String bookId, String supplierId, Integer addNumber) throws IOException {
        //加载mybaits核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //添加进货订单信息到add_order表
        AddOrderMapper addOrderMapper = sqlSession.getMapper(AddOrderMapper.class);
        //ShowSupplierMapper supplierMapper = sqlSession.getMapper(ShowSupplierMapper.class);
        ShowBookMapper BookInfoMapper = sqlSession.getMapper(ShowBookMapper.class);

        AddOrder addOrder = null;
        BookInfo bookInfo = null;
        ServiceState serviceState = new ServiceState();

        try {
            //数据处理
            addOrder = new AddOrder();
            addOrder.setBookId(bookId);
            addOrder.setSupplierId(supplierId);
            addOrder.setAddNumber(addNumber);
            //根据bookid获取书籍单价并计算总价格,
            bookInfo = BookInfoMapper.selectAllByBookId(bookId);
            Double addPrice = bookInfo.getBookPrice()*addNumber;
            addOrder.setAddPrice(addPrice);
            //生成订单日期
            String addTime = Handle.getDateAndTime();
            addOrder.setAddTime(addTime);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //System.out.println(addOrder);
            //System.out.println(bookInfo);
        }

        try {
            //上传订单信息
            addOrderMapper.addOrder(addOrder);
            //添加书本库存并更新数据库
            bookInfo.setBookQty(bookInfo.getBookQty()+addNumber);
            BookInfoMapper.updateBookInfo(bookInfo);

            //提交事务
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //回滚
            sqlSession.rollback();
        } finally {

        }


        sqlSession.close();

        return serviceState;
    }
}
